const { Router } = require("express");
const { SuccessResult, ErrorResult } = require("../utils");

const YRouter = Router();

YRouter.get("/username", (req, res) => {
  if(!req.session.isLogin) {
    return res.send(ErrorResult("请先登录！"))
  }

  res.send(SuccessResult("请求成功", req.session.user.username));
});

YRouter.post("/logout", (req, res) => {
  req.session.destroy();
  res.send(SuccessResult("操作成功！"));
});

/**
 * 登录接口
 */
YRouter.post("/login", (req, res) => {
  if(req.body.username !== 'admin' || req.body.passworld !== '123123') {
    return res.send(ErrorResult("登录失败！"))
  }

  req.session.user = req.body;
  req.session.isLogin = true;
 
  res.send(SuccessResult("登录成功！"));
})

module.exports = YRouter;